package com.hlc.jxls.common;

import net.sf.jxls.transformer.XLSTransformer;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-03-26.
 */
public class ExcelUitl {

    /**
     * 根据模板生成Excel文件.
     * @param templateFileName 模板文件.
     * @param list 模板中存放的数据.
     * @param resultFileName 生成的文件.
     */
    public void createExcel(String templateFileName, List<?> list, String resultFileName){
        try {
            //创建XLSTransformer对象
            XLSTransformer transformer = new XLSTransformer();

            //获取java项目编译后根路径
            URL url = this.getClass().getClassLoader().getResource("");

            //得到模板文件路径
            String srcFilePath = url.getPath() + templateFileName;
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("list", list);
            String destFilePath = url.getPath() + resultFileName;

            //生成Excel文件
            transformer.transformXLS(srcFilePath, map, destFilePath);
        } catch (Exception e) {
            throw new RuntimeException("error happens...", e);
        }
    }

   /* public String doExcel(String from, Map beans, String to) {
        System.out.println(ContextLoader.getCurrentWebApplicationContext().getServletContext().getContextPath());
        ServletContext sc = ContextLoader.getCurrentWebApplicationContext().getServletContext();
        String path = sc.getRealPath("/");
        XLSTransformer transformer =new XLSTransformer();
        String sfrom = path +"\\" + from;// 模板文件
        String sto = path +"\\" + to;// 要生成的文件
        try{
            transformer.transformXLS(sfrom, beans, sto);
        }catch (ParsePropertyException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return sto;
    }*/

    public void doDownLoad(String path, String name,HttpServletResponse response) {
        try{
            response.reset();
            response.setHeader("Content-disposition","attachment;success=true;filename ="+ URLEncoder.encode(name,"utf-8"));
            BufferedInputStream bis =null;
            BufferedOutputStream bos =null;
            OutputStream fos =null;
            InputStream fis =null;
            File uploadFile =new File(path);
            fis =new FileInputStream(uploadFile);
            bis =new BufferedInputStream(fis);
            fos = response.getOutputStream();
            bos =new BufferedOutputStream(fos);
            // 弹出下载对话框
            int bytesRead = 0;
            byte[] buffer =new byte[8192];
            while((bytesRead = bis.read(buffer, 0,8192)) != -1) {
                bos.write(buffer,0, bytesRead);
            }
            bos.flush();
            fis.close();
            bis.close();
            fos.close();
            bos.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
